﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class Administrator_Controls_PaggingControl : System.Web.UI.UserControl
{
    private Int32 _totalRecord;

    public Int32 TotalRecord
    {
        get { return _totalRecord; }
        set { _totalRecord = value; }
    }
    private Int32 _pageSize;

    public Int32 PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }
    private Int32 _pageIndex;

    public Int32 PageIndex
    {
        get { return _pageIndex; }
        set { _pageIndex = value; }
    }
    private String _pageURL;

    public String PageURL
    {
        get
        {
            if (_pageURL.Contains('?'))
            {
                return _pageURL + "&PageIndex=";
            }
            else
            {
                return _pageURL + "?PageIndex=";
            }
        }
        set { _pageURL = value; }
    }

    private Int32 _currentPageIndex = 1;

    public Int32 CurrentPageIndex
    {
        get { return _currentPageIndex; }
        set { _currentPageIndex = value; }
    }

    // Số lượng chỉ số trang hiện thị 1 lần
    private Int32 _pagingSize = 10;

    public Int32 PagingSize
    {
        get { return _pagingSize; }
        set { _pagingSize = value; }
    }
    public Boolean IsVN = true;
    private String languageCode = "vi-VN";
    protected void Page_Load(object sender, EventArgs e)
    {
        PageIndex = TotalRecord / PageSize;

        if (Request.QueryString["PageIndex"] != null)
        {
            CurrentPageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
        }
        if (!Page.IsPostBack)
        {
            try
            {
                languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                if (languageCode == "en-us")
                {
                    IsVN = false;
                }

            }
            catch (Exception ex)
            {
            }
        }
    }
    protected override void Render(HtmlTextWriter writer)
    {
        Int32 numberOfPage = TotalRecord / _pageSize + 1;

        if (TotalRecord % _pageSize == 0)
        {
            numberOfPage--;
        }

        Int32 start = CurrentPageIndex / _pagingSize;

        if (CurrentPageIndex % _pagingSize == 0)
        {
            start--;
        }

        start = start * _pagingSize + 1;

        if (start < 1)
        {
            start = 1;
        }

        Int32 stop = start + _pagingSize;

        if (stop > numberOfPage)
        {
            stop = numberOfPage + 1;
        }

        StringBuilder sb = new StringBuilder();
        Int32 firstIndex = 1;
        Int32 lastIndex = numberOfPage;
        Int32 previousIndex = start - 1;
        Int32 nextIndex = stop;

        if (previousIndex > 1)
        {
            sb.Append(" <a href='" + PageURL + firstIndex.ToString() + "'>&lt;&lt;</a>");
            sb.Append(" <a href='" + PageURL + previousIndex.ToString() + "'>&lt;</a>");
        }

        if(IsVN==true)
        {
             sb.Append("Trang:[" + CurrentPageIndex.ToString() + "]");
        }
        else
             sb.Append("Page:[" + CurrentPageIndex.ToString() + "]");
        sb.Append(RenderTheNumber(start, stop));

        if (nextIndex < numberOfPage)
        {
            sb.Append(" <a href='" + PageURL + nextIndex.ToString() + "'>&gt;</a>");
            sb.Append(" <a href='" + PageURL + lastIndex.ToString() + "'>&gt;&gt</a>");
        }

        if (TotalRecord > 0)
        {
            writer.Write(sb.ToString());
        }
        else
        {
            writer.Write("");
        }

        base.Render(writer);
    }

    private String RenderTheNumber(Int32 start, Int32 stop)
    {
        StringBuilder sb = new StringBuilder();

        for (Int32 i = start; i < stop; i++)
        {
            string currentNumberPagerCss = (i == CurrentPageIndex) ? "RedColor" : "";
            sb.Append(" <a href='" + PageURL + i.ToString() + "'>" + "<span class='" + currentNumberPagerCss + "'>" + i.ToString() + "</span></a>");
        }

        return sb.ToString();
    }
}
